package com.ltcode.auth.infrastructure.converter;

import cn.hutool.core.bean.BeanUtil;
import com.ltcode.auth.domain.model.AuthUser;
import com.ltcode.auth.infrastructure.remote.feign.model.SysUserDTO;
import com.ltcode.auth.infrastructure.remote.feign.model.SysUserDomain;
import org.springframework.beans.BeanUtils;

/**
 * 认证用户转换器
 * 
 * @author ltcode
 * @since 2024-01-01
 */
public class AuthUserConverter {

    /**
     * 从系统用户领域模型转换为认证用户
     */
    public static AuthUser fromSysUserDomain(SysUserDomain sysUser) {
        if (sysUser == null) {
            return null;
        }
        
        AuthUser authUser = new AuthUser();
        authUser.setId(sysUser.getId());
        authUser.setUsername(sysUser.getUserName());
        authUser.setNickName(sysUser.getNickName());
        authUser.setEmail(sysUser.getEmail());
        authUser.setSex(sysUser.getSex());
        authUser.setAvatar(sysUser.getAvatar());
        authUser.setPassword(sysUser.getPassword());
        authUser.setStatus(sysUser.getStatus());
        authUser.setDelFlag(sysUser.getDelFlag());
        authUser.setLoginIp(sysUser.getLoginIp());
        authUser.setLoginDate(sysUser.getLoginDate());
        authUser.setPwdUpdateDate(sysUser.getPwdUpdateDate());
        authUser.setCreateTime(sysUser.getCreateTime());
        authUser.setUpdateTime(sysUser.getUpdateTime());
        authUser.setDeptId(sysUser.getDeptId());
        
        return authUser;
    }

    /**
     * 从认证用户转换为系统用户DTO
     */
    public static SysUserDTO toSysUserDTO(AuthUser authUser) {
        if (authUser == null) {
            return null;
        }
        
        SysUserDTO userDTO = new SysUserDTO();
        BeanUtil.copyProperties(authUser, userDTO);
        return userDTO;
    }

    /**
     * 从认证用户转换为系统用户领域模型
     */
    public static SysUserDomain toSysUserDomain(AuthUser authUser) {
        if (authUser == null) {
            return null;
        }
        
        SysUserDomain userDomain = new SysUserDomain();
        BeanUtils.copyProperties(authUser, userDomain);
        userDomain.setId(authUser.getId());
        userDomain.setUserName(authUser.getUsername());
        
        return userDomain;
    }
}
